<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" :inline="true">
                <el-form-item label="用户类型" class="w-[180px]">
                    <el-select
                        v-model="queryParams.user_type"
                        clearable
                        placeholder="请选择用户类型"
                    >
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.user_type"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="旅行社" class="w-[260px]" prop="travel_agency_id">
                    <el-select
                        v-model="queryParams.travel_agency_id"
                        clearable
                        placeholder="请选择旅行社"
                    >
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in optionsData.trave_agency"
                            :key="index"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="姓名" prop="real_name">
                    <el-input
                        class="w-[120px]"
                        v-model="queryParams.real_name"
                        clearable
                        placeholder="请输入姓名"
                    />
                </el-form-item>
                <el-form-item label="账号" prop="account">
                    <el-input
                        class="w-[120px]"
                        v-model="queryParams.account"
                        clearable
                        placeholder="请输入账号"
                    />
                </el-form-item>
                <el-form-item label="是否禁用" class="w-[160px]" prop="is_disable">
                    <el-select v-model="queryParams.is_disable" clearable placeholder="请选择:">
                        <el-option label="全部" value=""></el-option>
                        <el-option
                            v-for="(item, index) in dictData.system_disable"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['user/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button
                v-perms="['user/delete']"
                :disabled="!selectData.length"
                @click="handleDelete(selectData)"
            >
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="用户类型" prop="user_type_value" />
                    <el-table-column label="旅行社" prop="travel_agency_name" />
                    <el-table-column label="头像" prop="avatar">
                        <template #default="{ row }">
                            <el-image style="width: 50px; height: 50px" :src="row.avatar" />
                        </template>
                    </el-table-column>
                    <el-table-column label="姓名" prop="real_name" show-overflow-tooltip />
                    <el-table-column label="昵称" prop="nickname" show-overflow-tooltip />
                    <el-table-column label="账号" prop="account" show-overflow-tooltip />
                    <el-table-column label="电话" prop="mobile" show-overflow-tooltip />
                    <el-table-column label="性别" prop="sex_value" />
                    <el-table-column label="余额" prop="user_money" show-overflow-tooltip />
                    <el-table-column label="是否禁用" prop="is_disable">
                        <template #default="{ row }">
                            <el-tag class="ml-2" :type="row.is_disable ? 'danger' : 'primary'"
                                >{{ row.is_disable_value }}
                            </el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="最后登录IP" prop="login_ip" show-overflow-tooltip />
                    <el-table-column label="最后登录时间" prop="login_time" show-overflow-tooltip />

                    <el-table-column label="操作" width="260" fixed="right">
                        <template #default="{ row }">
                            <el-button
                                v-if ="row.user_type == 3"
                                v-show="true"
                                v-perms="['user.driver_credential/edit']"
                                type="success"
                                link
                                @click="handleDriver(row.driver, row.id)"
                            >
                               <div v-if="row.driver_status == ''" style="color:red">证件审核</div>
                               <div v-else>证件查看</div>
                            </el-button>
                            <el-button
                                v-if ="row.user_type != 1"
                                v-perms="['user.user/adjustMoney']"
                                type="primary"
                                link
                                @click="handleAdjust(row.user_money, row.id)"
                            >
                                余额调整
                            </el-button>
                            <el-button
                                v-perms="['user/edit']"
                                type="primary"
                                link
                                @click="handleEdit(row)"
                            >
                                编辑
                            </el-button>
                            <el-button
                                v-perms="['user/delete']"
                                type="danger"
                                link
                                @click="handleDelete(row.id)"
                            >
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup
            v-if="showEdit"
            ref="editRef"
            :dict-data="dictData"
            @success="getLists"
            @close="showEdit = false"
        />
        <account-adjust
            v-model:show="adjustState.show"
            :value="adjustState.value"
            @confirm="handleConfirmAdjust"
        />
        <driver-credential
            v-model:show="driverState.show"
            :value="driverState.value"
            @confirm="handleConfirmDriver"
        />
    </div>
</template>

<script lang="ts" setup name="userLists">
import {usePaging} from '@/hooks/usePaging'
import {useDictData, useDictOptions} from '@/hooks/useDictOptions'
import {apiUserLists, apiUserDelete,apiDriverCredentialAdd,apiDriverCredentialEdit} from '@/api/user'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {travelAgencyAll} from '@/api/travel_agency.ts'
import {getRoutePath} from '@/router'
import AccountAdjust from '@/views/consumer/components/account-adjust.vue'
import {adjustMoney} from '@/api/consumer.ts'
import DriverCredential from "@/views/consumer/components/driver-credential.vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

// 查询条件
const queryParams = reactive({
  user_type: '',
  travel_agency_id: '',
  real_name: '',
  account: '',
  is_disable: ''
})

const adjustState = reactive({
  show: false,
  value: '',
  id: ''
})

const driverState = reactive({
  show: false,
  value: '',
  id: ''
})


const handleAdjust = (value: string, id: number) => {
  adjustState.show = true
  adjustState.value = value
  adjustState.id = id
}

const handleDriver = (value: string, id: number) => {
  driverState.show = true
  driverState.value = value
  driverState.id = id
}

const handleConfirmAdjust = async (value: any) => {
  await adjustMoney({user_id: adjustState.id, ...value})
  adjustState.show = false
  getLists()
}

const handleConfirmDriver = async (value: any) => {
  if(value.id == '')
  {
    await apiDriverCredentialAdd({user_id: driverState.id, ...value})
  }else{
    await apiDriverCredentialEdit({user_id: driverState.id, ...value})
  }
  driverState.show = false
  getLists()
}

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('user_type,sex,system_disable')

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiUserLists,
  params: queryParams
})

const {optionsData} = useDictOptions<{
  trave_agency: any[]
}>({
  trave_agency: {
    api: travelAgencyAll
  }
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}



// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiUserDelete({id})
  getLists()
}

getLists()
</script>
